package net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import mytool.Time;

public class UrlTest {
	public static void main(String[] args) throws IOException {
		test();
	}

	private static void test() throws IOException {
		//Exception in thread "main" java.net.SocketException: Network is unreachable: connect
		//URL url= new URL("http://www.google.com/");//Exception in thread "main" java.net.ConnectException: Connection timed out: connect
		URL url = new URL("http://student.tarena.com.cn/");
		Time.start();
		URLConnection con = url.openConnection();
		con.connect();
		Time.end();
		BufferedReader in = new BufferedReader(new InputStreamReader(con
				.getInputStream(),"gbk"));
		String s ;
		 while((s=in.readLine())!=null){
		 System.out.println(s);
		 }

		Map<String, List<String>> map = con.getHeaderFields();
		Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Entry<String, List<String>> e = it.next();
			System.out.println(e.getKey() + "," + e.getValue());
		}
	}
}
